package com.woniu.gf.pawn.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.gf.pawn.comm.core.domain.Product;
import com.woniu.gf.pawn.comm.core.domain.ProductBigType;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.product.parma.AddProductBrandParam;
import com.woniu.gf.pawn.product.parma.AddProductParam;
import com.woniu.gf.pawn.product.parma.FindProductBigTypePageParam;
import com.woniu.gf.pawn.product.parma.ListIdParam;
import com.woniu.gf.pawn.service.IProductService;
import com.woniu.gf.pawn.utils.BindingResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/product")
@Api(tags = "模板的控制类")
public class ProductController {


    @Autowired
    private IProductService productService;


    /**
     * 查询大类 map集合
     * @return
     */
    @ApiOperation("查询大类 map集合")
    @GetMapping("findBigType")
    public ResultDto findBigType(){
        return ResultDto.success(200,"大类查询",productService.findMap());
    }

    /**
     * 根据大类id 查询品牌
     * @return
     */
    @ApiOperation("根据大类id 查询品牌")
    @GetMapping("findBrandType")
    public ResultDto findBrandType(Integer bigId){
        return ResultDto.success(200,"大类查询",productService.findByBigId(bigId));
    }
    /**
     * 新增product
     * @return
     */
    @ApiOperation("新增模板")
    @PostMapping("addProduct")
    public ResultDto addProduct(@Valid @RequestBody AddProductParam addProductParam, BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()){
            Product product = new Product();
            BeanUtils.copyProperties(addProductParam,product);
            productService.addProduct(product);
            return ResultDto.success(200,"新增product成功");
        }
       return ResultDto.fail(202,"新增失败",bindingResult);
    }

    /**
     * 查询分页
     */
    @ApiOperation("查询展示")
    @GetMapping("findPage")
    public ResultDto findPage(@Valid FindProductBigTypePageParam findProductBigTypePageParam, BindingResult result) {
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            IPage<Product> productBigTypeIPage = productService.selectPageVo(findProductBigTypePageParam);

            return ResultDto.success(200, "查询成功",productBigTypeIPage );
        }
        return ResultDto.fail(202, "查询失败",bindingResult );
    }

    /**
     * 删除模板
     */
    @ApiOperation("删除模板")
    @PostMapping("deleteProduct")
    public  ResultDto  deleteProduct(@RequestBody ListIdParam listIdParam)  {

        if(listIdParam.getIds()==null)  return  ResultDto.fail(202,"删除失败","参数为空");
         productService.deleteProduct(listIdParam.getIds());
        return  ResultDto.success(200,"删除成功");
    }
}

